﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using System.Collections.Concurrent;
using Microsoft.Office.Interop.Excel;
using SUNRPA.CONTROLS.IMPL.util;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;

namespace SUNRPA.CONTROLS.IMPL.controls {
    /// <summary>
    /// TODO:1、目前只支持对整个sheet作为数据源。将来添加自定义区域
    /// </summary>
    public class NewPivotTableNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            NewPivotTableNParam? param = item.ControlParam as NewPivotTableNParam;
            string excelObj = param.ExcelObject;
            Workbook wb = this.GetObjVar<ExcelModel>(ref excelObj, globleData).WorkBookValue as Workbook;
            if(wb == null) { 
                throw new RpaRuntimeException(String.Format("Excel对象{0}不存在", excelObj));
            }

            ExcelUtil excelUtil = new ExcelUtil();
            excelUtil.SetApp(wb);

            string sourceSheet = param.SourceSheet;
            string targetSheet = param.TargetSheet;
            string targetRow = param.TargetRow;
            string targetColumn = param.TargetColumn;
            string pivotName = param.PiovtName;
            string rowFielsds = param.RowFields;
            string columnFields = param.ColumnFields;

            this.GetStringVar(ref sourceSheet, globleData);
            this.GetStringVar(ref targetSheet, globleData);
            this.GetStringVar(ref targetRow, globleData);
            this.GetStringVar(ref targetColumn, globleData);
            this.GetStringVar(ref pivotName, globleData);
            this.GetStringVar(ref rowFielsds, globleData);
            this.GetStringVar(ref columnFields, globleData);
            
            List<string> valAndType = param.ValueAndCalTypes;

            excelUtil.CreatePiovtTable(sourceSheet, targetSheet, targetRow, targetColumn, pivotName, rowFielsds, columnFields, valAndType);
            return currentIndex;
        }
    }
}
